package com.applancer.adapter;

import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.applancer.model.FeedModel;
import com.applancer.model.GeneralModel;
import com.applancer.nativecmsflip.R;
import com.squareup.picasso.Picasso;

public class HomeFeedAdapter extends BaseExpandableListAdapter {

	protected List<GeneralModel> ldata;

	public HomeFeedAdapter(Context context, List<GeneralModel> objects) {
		ldata = objects;
	}

	@Override
	public int getGroupCount() {
		return ldata.size() / 3;
	}

	@Override
	public int getChildrenCount(int groupPosition) {

		return 2;
	}

	@Override
	public Object getGroup(int groupPosition) {

		return ldata.get(groupPosition * 3);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {

		return ldata.get(groupPosition * 3 + childPosition + 1);
	}

	@Override
	public long getGroupId(int groupPosition) {
		String str = ((FeedModel) ldata.get(groupPosition * 3)).getId();
		return Integer.parseInt(str);

	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		String str = ((FeedModel) ldata.get(groupPosition * 3 + childPosition
				+ 1)).getId();
		return Integer.parseInt(str);
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, final ViewGroup parent) {

		final FeedModel model = (FeedModel) getGroup(groupPosition);

		final ViewHolderBig holder;

		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(parent.getContext());

			convertView = inflater.inflate(R.layout.adapter_feed_big, null);

			holder = new ViewHolderBig();
			holder.thumb_big = (ImageView) convertView
					.findViewById(R.id.thumb_id);
			holder.textview_title_big = (TextView) convertView
					.findViewById(R.id.title_id);
			holder.textview_author_big = (TextView) convertView
					.findViewById(R.id.author_id);

			// holder.textComment_big = (TextView) convertView
			// .findViewById(R.id.feed_comment_id);
			// holder.textLike_big = (TextView) convertView
			// .findViewById(R.id.feed_like_id);
			// holder.textShare_big = (TextView) convertView
			// .findViewById(R.id.feed_share_id);
			holder.progressBar = (ProgressBar) convertView
					.findViewById(R.id.progressBar);
			holder.cat_name = (TextView) convertView
					.findViewById(R.id.cat_name);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolderBig) convertView.getTag();
		}

		try {
			holder.textview_title_big.setText(model.getTitle());
		} catch (Exception e) {

		}

		if (groupPosition == 0)
			convertView.setPadding(0, 0, 0, 0);
		else
			convertView.setPadding(0, 25, 0, 0);

		holder.cat_name.setText(model.getCategory());
		// holder.progressBar.setVisibility(View.VISIBLE);
		holder.textview_author_big.setText(model.getDate());
		// holder.textComment_big
		// .setText(((model.getCountCommentFB().equals("")) ? "0" : model
		// .getCountCommentFB()) + " comment");
		// holder.textLike_big.setText(((model.getCountLikeFB().equals("")) ?
		// "0"
		// : model.getCountLikeFB()) + " like");
		// holder.textShare_big
		// .setText(((model.getCountShareFB().equals("")) ? "0" : model
		// .getCountShareFB()) + " share");

		(new Handler()).postDelayed(new Runnable() {

			@Override
			public void run() {

				Picasso.with(parent.getContext())
						.load(model.getUrlImageThumb())
						.placeholder(R.drawable.ic_launcher)
						.error(R.drawable.ic_launcher).fit().centerCrop()
						.into(holder.thumb_big);

				// Picasso.with(parent.getContext())
				//
				// .load(model.getUrlImageThumb()).fit().centerCrop()
				//
				// .into(holder.thumb_big);
				// new ImageLoadedCallback(holder.progressBar) {
				// @Override
				// public void onSuccess() {
				// if (holder.progressBar != null) {
				// holder.progressBar
				// .setVisibility(View.GONE);
				// }
				// }
				//
				// @Override
				// public void onError() {
				// super.onError();
				//
				// }
				//
				// });
			}
		}, 20);

		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		FeedModel model = (FeedModel) getChild(groupPosition, childPosition);

		final ViewHolder holder;

		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(parent.getContext());

			convertView = inflater.inflate(R.layout.adapter_feed, null);

			holder = new ViewHolder();
			holder.thumb = (ImageView) convertView.findViewById(R.id.thumb_id);
			holder.textview_title = (TextView) convertView
					.findViewById(R.id.title_id);
			holder.textview_author = (TextView) convertView
					.findViewById(R.id.author_id);

			// holder.textComment = (TextView) convertView
			// .findViewById(R.id.feed_comment_id);
			// holder.textLike = (TextView) convertView
			// .findViewById(R.id.feed_like_id);
			// holder.textShare = (TextView) convertView
			// .findViewById(R.id.feed_share_id);
			holder.progressBarsmall = (ProgressBar) convertView
					.findViewById(R.id.progressBarsmall);

			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		try {
			holder.textview_title.setText(model.getTitle());
		} catch (Exception e) {

		}

		// holder.progressBarsmall.setVisibility(View.VISIBLE);
		holder.textview_author.setText(model.getDate());
		// holder.textComment
		// .setText(((model.getCountCommentFB().equals("")) ? "0" : model
		// .getCountCommentFB()) + " comment");
		// holder.textLike.setText(((model.getCountLikeFB().equals("")) ? "0"
		// : model.getCountLikeFB()) + " like");
		// holder.textShare.setText(((model.getCountShareFB().equals("")) ? "0"
		// : model.getCountShareFB()) + " share");

		Picasso.with(parent.getContext()).load(model.getUrlImageThumb())
				.placeholder(R.drawable.ic_launcher)
				.error(R.drawable.ic_launcher).fit().centerCrop()
				.into(holder.thumb);

		// Picasso.with(parent.getContext())
		// .load(model.getUrlImageThumb())
		// .fit()
		// .centerCrop()
		// .into(holder.thumb,
		// new ImageLoadedCallback(holder.progressBarsmall) {
		// @Override
		// public void onSuccess() {
		// if (holder.progressBarsmall != null) {
		// holder.progressBarsmall
		// .setVisibility(View.GONE);
		// }
		// }
		//
		// @Override
		// public void onError() {
		// super.onError();
		//
		// }
		//
		// });

		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	static class ViewHolder {
		ImageView thumb;
		TextView textview_title;
		TextView textview_author;
		// TextView textComment;
		// TextView textLike;
		// TextView textShare;
		ProgressBar progressBarsmall;
	}

	static class ViewHolderBig {
		ImageView thumb_big;
		TextView textview_title_big;
		TextView textview_author_big;
		// TextView textComment_big;
		// TextView textLike_big;
		// TextView textShare_big;
		ProgressBar progressBar;
		TextView cat_name;
	}

}
